package g.test;

import java.io.FileInputStream;
import java.util.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.*;
import excel.Excel;


public class Gtest {
  
	/*Property File Variables*/
	String email = "";
	String password = "";
	int maxEmail;
	
	/* URL maintaining variables */
	String baseURL = "";
	String refURL = "";
	String currentURL="";
	
	/* Company Detail maintaining variables */
	String companyEmail="";
	String companyAddress="";
	String companyNumber="";
	String companyName="";
	
	/* File handling variable */
	Excel excel;
	
	/* Browser */
	WebDriver driver;
	
	/* Wait time providing variables */
	int modular;
	int minimum;
	
	/* Execution controlling variables*/
	int currentEmail;
	int page;
	
	/* Random number generating variable */
	Random rn = new Random();
	
	/* Properties file variable */
	Properties props = new Properties();
	
	@BeforeClass
	public void initialize() throws Exception{
		
        FileInputStream config = new FileInputStream("config.properties");
        props.load(config);
		excel = new Excel();
		email = props.getProperty("email1");
		password = props.getProperty("password");
		maxEmail = Integer.parseInt(props.getProperty("maxEmail"));
		currentEmail = 1;
		baseURL = "https://www.gmail.com";
		page = 1;
		minimum = 2;
		modular = 4;
		
	}
	
	/*
	 * It provides a sleeping time for thread
	 */
	public long waitTime() {
		
		long time = rn.nextInt() % modular;
		if( time < 0 )
			time = time * -1;
		time = time + minimum;
		return time;
		
	}
	
	/*
	 *It provides new values to execute 
	 */
	public boolean getNewValues() {
		
		currentEmail = currentEmail + 1;
		if(currentEmail > maxEmail) {
			return false;
		}
		email = props.getProperty("email"+currentEmail);
		return true;
		
	}
	
	/*
	 * manages a session
	 */
	@Test
	public void login() throws Exception {
		
		while(getNewValues()) {
			
			startBrowser();
			WebElement uName = driver.findElement(By.id("Email"));
			WebElement uPassword = driver.findElement(By.id("Passwd"));
			WebElement signIn = driver.findElement(By.id("signIn"));
			uName.sendKeys(email);
			uPassword.sendKeys(password);
			signIn.click();
			Thread.sleep(50 * 1000);
			refURL = driver.getCurrentUrl();
			refURL = refURL + "/p";
			changeURL();
			openMails();
			List<WebElement> mailFolder = driver.findElements(By.xpath("//a[contains(@class,'J-Ke')]"));
			baseURL = mailFolder.get(8).getAttribute("href");
			driver.get(baseURL);
			Thread.sleep((waitTime() + 5) * 1000);
			refURL = driver.getCurrentUrl();
			refURL = refURL + "/p";
			changeURL();
			openMails();
			closeBrowser();
			
		}
		
	}
	
	/*
	 * starts the browser for new session
	 */
	public void startBrowser() {
		
		driver  = new FirefoxDriver();
		driver.get(baseURL);
		
	}
	
	/*
	 * close the session by closing the browser
	 */
	public void closeBrowser() {
		driver.close();
	}
	
	public void changeURL()
	{
		baseURL = refURL + page;
	}
	
	/*
	 * it opens the mail checks for valid unread mail whose data can be extracted
	 */
	public void openMails() throws Exception {
		
		List<WebElement> mails = driver.findElements(By.xpath("//span[@class='zF']"));
		String [] s = { "Just Dial"};
		int count;
		while(mails.size()!= 0) {
			
			currentURL = driver.getCurrentUrl();
			count = 0;
			while( (mails.size() - count) != 0) {
				
				if(s[0].equalsIgnoreCase(mails.get(count).getText())) {
					
					mails.get(count).click();
					Thread.sleep(waitTime() * 1000);
					fetchJDData();
					driver.navigate().back();
					Thread.sleep(500);
					mails = driver.findElements(By.xpath("//span[@class='zF']"));
					
				}
				else {
					count++;
				}
				
			}
			page = page + 1;
			changeURL();
			driver.get(baseURL);
			Thread.sleep(1000 * 60);
			if(currentURL.equalsIgnoreCase(driver.getCurrentUrl())) {
				break;
			}
			mails = driver.findElements(By.xpath("//span[@class='zF']"));
			
		}
		Thread.sleep((waitTime() - minimum + 1) * 1000 * 60);
	}
	
	/*
	 * It fetches the data for the JD mail format only 
	 */
	public void fetchJDData() throws Exception {
		
		String buffer1;
		int i = 0;
		List<WebElement> data = driver.findElements(By.xpath("//td[@style='font-family:Verdana;font-size:10px;color:#343224;text-decoration:none' and @valign='top']"));
		companyEmail = "";
		companyName = "";
		while(i != data.size()) {
			
			buffer1 = data.get(i).getText();
			if(i == 0) {
				companyAddress = buffer1;
			} else if(buffer1.charAt(0) == '+') {
				companyNumber = buffer1;
			} else{
				companyEmail = buffer1;
			}
			i = i + 1;
		}
		List<WebElement> company = driver.findElements(By.xpath("//a[@style='text-decoration:none' and @target='_blank']"));
		if(company.size()>0) {
			
			buffer1 = company.get(company.size()-1).getText();
			for( i = 0; i < buffer1.length(); i = i + 1) {
				
				if(buffer1.charAt(i)=='\'')
					break;
			}
			for(i = i +1; i < buffer1.length(); i = i + 1) {
				
				if(buffer1.charAt(i) == '\'') {
					break;
				}
				companyName = companyName + buffer1.charAt(i);
			}
			
		}
		//excel.insertRow(companyName, companyAddress, companyNumber,  companyEmail);
	}
}
